package android.gpsmessenger;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class MessageListFragment extends Fragment
{
	private MessageList messages;
	private String xmlFile;
	private boolean saveData = true;
	
	EditText sosMessage_field ;
	EditText helpMessage_field ;
	EditText message1_field  ;
	EditText message2_field  ;
	
	public void onCreate (Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		
		messages = new MessageList();
		xmlFile = getActivity().getFilesDir().getAbsolutePath() + "/messages.xml";
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		return inflater.inflate(R.layout.fragment_config_messages, container, false);
	}

	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
	{
		inflater.inflate(R.menu.fragment_ok_cancel_menu, menu);
	}
	
	public void onResume() 
	{
		super.onResume();
		saveData = true;

		messages.load(xmlFile);
		
		if (messages != null) 
		{
			sosMessage_field  = (EditText) getActivity().findViewById(R.id.sos_message);
			helpMessage_field = (EditText) getActivity().findViewById(R.id.help_message);
			message1_field    = (EditText) getActivity().findViewById(R.id.message1);
			message2_field    = (EditText) getActivity().findViewById(R.id.message2);
			
			sosMessage_field.setText(messages.getSOSMessage().getMessage());
			helpMessage_field.setText(messages.getHelpMessage().getMessage());
			message1_field.setText(messages.getMessage1().getMessage());
			message2_field.setText(messages.getMessage2().getMessage());
		}
	}

	public void onPause() 
	{
		super.onPause();
		if (saveData) 
		{			
			sosMessage_field  = (EditText) getActivity().findViewById(R.id.sos_message);
			helpMessage_field = (EditText) getActivity().findViewById(R.id.help_message);
			message1_field    = (EditText) getActivity().findViewById(R.id.message1);
			message2_field    = (EditText) getActivity().findViewById(R.id.message2);
			
			String sosMessage  = sosMessage_field.getText().toString();
			String helpMessage = helpMessage_field.getText().toString();
			String message1    = message1_field.getText().toString();
			String message2    = message2_field.getText().toString();
			
			messages.setSOSMessage(new Message(sosMessage));
			messages.setHelpMessage(new Message(helpMessage));
			messages.setMessage1(new Message(message1));
			messages.setMessage2(new Message(message2));
			
			messages.save(xmlFile);
		}
	}
	
	public boolean onOptionsItemSelected (MenuItem item) 
	{
		switch (item.getItemId()) 
		{
		 	case R.id.menu_ok:
		 		if(validated()) 
		 		{
		 			saveData = true;
		 			getActivity().finish();
		 		} 
		 		else 
		 		{
		 			Toast.makeText(getActivity(), 
		 			"You have missing or incorrect values in some form fieds!", Toast.LENGTH_SHORT).show();
		 		}
		 		break;
		 	case R.id.menu_cancel:
		 		saveData = false;
		 		getActivity().finish();
		 		break;
		 }
		return true;
	}
	
	protected boolean validated() 
	{		  
		boolean validated = true;
		
		sosMessage_field  = (EditText) getActivity().findViewById(R.id.sos_message);
		helpMessage_field = (EditText) getActivity().findViewById(R.id.help_message);
		message1_field    = (EditText) getActivity().findViewById(R.id.message1);
		message2_field    = (EditText) getActivity().findViewById(R.id.message2);
		  
		if (!Validate.hasText(sosMessage_field)) validated = false;
		if (!Validate.hasText(helpMessage_field)) validated = false;
		if (!Validate.hasText(message1_field)) validated = false;
		if (!Validate.hasText(message2_field)) validated = false;
		
		return validated;
	}		

}
